/**
 *
 * @author 
 *
 */
class EnemyManage extends egret.EventDispatcher {
    private enterFrameCount: number = 0;
    private FrameCount: number = 0;
    private enemyCount: number = 0;
    private objCon: egret.DisplayObjectContainer;
    public armyList: ArmyBase[] = [];
    private armyMax: number = 51;
    public allStopflg: boolean = false;
	
    private static _i: EnemyManage;
    public static get i(): EnemyManage {
        if(!this._i)
            this._i = new EnemyManage();
        return this._i;
    }
    public constructor() {
        super();
    }
		
    public setup(obj: egret.DisplayObjectContainer): void {
        this.objCon = obj;
    }
    public enterFrame(): void {
        this.create();
        this.control();
    }
	
    public GameReStart(): void {
		this.enterFrameCount = 0;
		this.FrameCount = 0;
		this.enemyCount = 0;
		this.objCon.removeChildren();
		this.armyList = [];
		this.allStopflg = false;
    }
    private create(): void {
        if(this.enterFrameCount - this.enemyCount >= 0) {
            if(this.armyList.length < this.armyMax) {
                var army = new ArmyBase();
                army.data = new ArmyInfo();
                army.x = 40 + 400 * Math.random();
                army.y = 50;
                army.alpha = 0;
                var tw = egret.Tween.get(army);
                tw.to({ alpha: 1 },1000,egret.Ease.sineIn);
                this.objCon.addChild(army);
                this.armyList.push(army);
                this.enemyCount = this.enterFrameCount + 100 + Math.random() * 50 +this.FrameCount;
				this.FrameCount -= 5;
				if(this.FrameCount <= -100) {
					this.FrameCount = 0;
				}
            }
        }
    }
    public AllPlay(): void {
        this.allStopflg = false;
        for(var i in this.armyList) {
            var armyTemp = this.armyList[i];
            armyTemp.play();
        }
	}
    public AllStop(): void {
        this.allStopflg = true;
        for(var i in this.armyList) {
            var armyTemp = this.armyList[i];
            armyTemp.stop();
        }
	}
    private control(): void {
        //交换位置
        if(this.enterFrameCount % 20 == 0) {
            for(var m: number = 1;m < this.objCon.numChildren;m++) {
                var children = this.objCon._children[m];
                var childrenPre = this.objCon._children[m - 1];
                if(children.y < childrenPre.y) {
                    if(this.objCon.getChildIndex(children) > this.objCon.getChildIndex(childrenPre)) {
                        this.objCon.swapChildren(children,childrenPre);
                    }
                }
            }
        }
		
        if(BuildManage.i.MyZhuBuildList.length == 0) return;
        for(var j in this.armyList) {
            var armyTemp: ArmyBase = this.armyList[j];
            if(armyTemp.y > GameConfig.curHeight()) {
                this.armyList.splice(j,1);
                this.objCon.removeChild(armyTemp);
                armyTemp = null;
                continue;
            } else if(armyTemp.data.bloodVolume <= 0) {
                var getGold: number = armyTemp.data.gold;
                GameManager.i.gold = GameManager.i.gold + Math.round(getGold * 0.9 + getGold * 0.2 * Math.random());
                Global.dispatchEvent(Events.GlobalGold);
                this.armyList.splice(j,1);
                this.objCon.removeChild(armyTemp);
                armyTemp = null;
                continue;
            }
            if(this.allStopflg) return;
            if(armyTemp.data.iceCount > 0) {
                armyTemp.data.iceCount--;
            } else {
                armyTemp.data.iceCount = 0;
            }

            var buildTemp;
            for(var i in BuildManage.i.MyZhuBuildList) {
				/*
				
				*/
                buildTemp = BuildManage.i.MyZhuBuildList[i];
            }
            if(armyTemp.data.iceCount > 0) {
                armyTemp.stop();
            } else if(armyTemp.y < buildTemp.y - 40) {
                armyTemp.y += armyTemp.data.speed;
                armyTemp.setDirection(new egret.Point(armyTemp.x,armyTemp.y + 1));
                armyTemp.play();
            } else if(armyTemp.x > (buildTemp.x - 21) && armyTemp.x < (buildTemp.x + buildTemp.width - 33) && armyTemp.y > (buildTemp.y - 40) && armyTemp.y < (buildTemp.y + buildTemp.height + 32)) {
                armyTemp.setDirection(new egret.Point(armyTemp.x,armyTemp.y + 1));
                if(armyTemp.data.prehitTime == 0) {
                    armyTemp.data.prehitTime = this.enterFrameCount;
                    armyTemp.stop();
                }
                else if(((this.enterFrameCount - armyTemp.data.prehitTime) > armyTemp.data.hitDelayTime) && armyTemp.data.prehitTime != 0) {
                    armyTemp.data.ismove = true;
                    armyTemp.data.prehitTime = this.enterFrameCount;
                    if(buildTemp.data.bloodVolume > 0 && armyTemp.data.attackTime == 0) {
                        armyTemp.attack();
                    }
                }
            } else {
                var buildPoint: egret.Point = new egret.Point(buildTemp.x,buildTemp.y - 32);
                var targetSpeed: egret.Point = CommonFunction.GetSpeed(buildPoint,new egret.Point(armyTemp.x,armyTemp.y),armyTemp.data.speed);
                armyTemp.x = armyTemp.x + targetSpeed.x;
                armyTemp.y = armyTemp.y + targetSpeed.y;
                armyTemp.setDirection(buildPoint);
                armyTemp.play();
                armyTemp.data.prehitTime = 0;
            }

            if(armyTemp.data.lifeType == "_melee") {
                armyTemp.data.attackTime++;
                if(armyTemp.data.attackTime >= 70) {
                    armyTemp.data.attackTime = 0;
                    armyTemp.normal();
                    buildTemp.hit(armyTemp.data.hitBlood);
                }
            }
        }
        this.enterFrameCount++;
    }
}
